package com.yitong.base.dao.impl;

import java.util.HashMap;
import java.util.Map;

import com.yitong.base.dao.api.BaseService;
import com.yitong.base.domain.Message;
import com.yitong.base.util.Tools;

public class BaseServiceImpl implements BaseService {
	public static  Map<Integer,Object> mapException = null;
	public final static int CODE_SUCCESS = 0;//0	成功
	public final static int CODE_FAIL = 1;//0	失败
	public final static int CODE_MD5_ERROR = 100001;//100001	比对md5失败错误
	public final static int CODE_PROPERTY_NULL = 100002;//100002	参数为空或错误
	public final static int CODE_PROPERTY_FRMANT_EXCEPTION = 1000021;//100002	参数为空或错误
	public final static int CODE_DB_ERROR = 100003;//100003	数据库操作错误
	public final static int CODE_DB_NULL = 1000031;//100003	查询结果不存在
	public final static int CODE_CACHE_ERROR = 100004;//100004	缓存操作错误
	public final static int CODE_APPLICATION_NOT_EXISTS = 100005;//100005	该程序不存在
	public final static int CODE_APPLICATION_ERROR = 100006;//100006	该程序不可用
	public final static int CODE_METHOD_NOT_EXISTS = 100007;//100007	该动作不存在
	public final static int CODE_IPADDRESS_NOT_EXISTS = 100008;//100008	IP不属于服务网段
	public final static int CODE_INTERFACE_ERROR = 100009; 	//调用接口出错
	public final static int CODE_ACTIVITY_NOT_EXISTS = 100010; //活动已失效过期或不存在
	public final static int CODE_EXCEPTION = 999998;//999998	未知错误
	public final static int CODE_OTHER_ERROR = 999999;//999999	未知错误
	public static String PAGE_NO = "pageNo";
	
	public static String getMessageValue(Integer key){
		String value = null;
		if(null == mapException){
			mapException = new HashMap<Integer,Object>();
			mapException.put(CODE_SUCCESS, "成功");
			mapException.put(CODE_FAIL, "失败");
			mapException.put(CODE_MD5_ERROR, "比对md5失败错误");
			mapException.put(CODE_PROPERTY_NULL, "参数为空或不完整");
			mapException.put(CODE_PROPERTY_FRMANT_EXCEPTION, "参数格式不正确");
			mapException.put(CODE_DB_ERROR, "数据库操作错误");
			mapException.put(CODE_DB_NULL, "查询结果不存在");
			mapException.put(CODE_CACHE_ERROR, "缓存操作错误");
			mapException.put(CODE_APPLICATION_NOT_EXISTS, "该程序不存在");
			mapException.put(CODE_APPLICATION_ERROR, "该程序不可用");
			mapException.put(CODE_METHOD_NOT_EXISTS, "该动作不存在");
			mapException.put(CODE_IPADDRESS_NOT_EXISTS, "IP不属于服务网段");
			mapException.put(CODE_INTERFACE_ERROR, "调用接口出错");
			mapException.put(CODE_ACTIVITY_NOT_EXISTS, "活动已失效过期或不存在");
			mapException.put(CODE_EXCEPTION, "应用程序异常");
			mapException.put(CODE_OTHER_ERROR, "未知错误");
		}
		if(  key < 0){
			value =	mapException.get(CODE_OTHER_ERROR).toString();
		} else{
			value =	mapException.get(key).toString();
		}
			return value;
	}
	
	/***
	 * 检查参数 param 中是否包含指定的key
	 * @param param
	 * @param codes
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked", "static-access" })
	public Message isMessage(Map<String, Object> param,String ...codes) {
		boolean flag = false;
		Message   msg = new Message(CODE_PROPERTY_NULL,this.getMessageValue(CODE_PROPERTY_NULL),null);
		if(Tools.checkMap(param)){
			for(String code : codes){
				if(param.containsKey(code) ){
					flag = true;
			    }else{
			    	flag = false;
			    }
			}
		} 
		if(flag){
			msg = null;
		} 
		return msg;
	}
}
